package com.fishingwithme.android.ui.screens.map.screens

import android.Manifest
import android.content.pm.PackageManager
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.fishingwithme.android.ui.screens.map.TencentMapWrapper
import com.fishingwithme.android.ui.viewmodels.NavigatorViewModel
import java.util.UUID

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SpotNavUpdateScreen(
    navController: NavController,
    spotId: String,
    moduleKey: String,
    viewModel: NavigatorViewModel = hiltViewModel()
) {
    // 获取ViewModel的UI状态
    val uiState by viewModel.uiState.collectAsState()
    var hasFetchedData by remember { mutableStateOf(false) }

    // 只在首次加载时获取导航信息
    if (!hasFetchedData) {
        viewModel.fetchSpotNavigationInfo(spotId)
        hasFetchedData = true
    }

    Scaffold(
    ) { innerPadding ->
        val context = LocalContext.current
        var permissionsGranted by remember { mutableStateOf(false) }

        // 权限请求器
        val permissionLauncher = rememberLauncherForActivityResult(
            ActivityResultContracts.RequestMultiplePermissions()
        ) { permissions ->
            permissionsGranted = permissions.all { it.value }
        }

        // 检查权限
        DisposableEffect(Unit) {
            val requiredPermissions = arrayOf(
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )

            val allGranted = requiredPermissions.all {
                ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
            }

            if (!allGranted) {
                permissionLauncher.launch(requiredPermissions)
            } else {
                permissionsGranted = true
            }

            onDispose {}
        }

        // 只有当导航数据可用时才显示地图组件
        uiState.navLine?.let { navLine ->
            val data = remember(navLine) {
                mutableMapOf<String, Any>("line" to navLine)
            }

            // 地图组件
            TencentMapWrapper(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(innerPadding),
                permissionsGranted = permissionsGranted,
                moduleKey = moduleKey,
                data = data,
                changeKey = UUID.randomUUID().toString(),
                onNavigateBack = {
                    navController.popBackStack()
                }
            )
        }
    }
}